package com.dao;

import com.entity.DvdInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/4/22 9:06
 **/
public class DvdInfoDaoImpl extends BaseDao implements DvdInfoDao {
	@Override
	public List<DvdInfo> findAll() {
		List<DvdInfo> list = new ArrayList<DvdInfo>();
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="Select *  from dvdInfo";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			//执行
			rs = psm.executeQuery();//查询==》返回的是，虚拟表==》sql的运行结果
			//处理返回结果
			while (rs.next()){
				DvdInfo info = new DvdInfo(
						rs.getInt("id"),
						rs.getString("state"),
						rs.getString("name"),
						rs.getString("borrowDate"),
						rs.getInt("borrowCount")
				);
				list.add(info);//添加到集合
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return list;
	}

	@Override
	public List<DvdInfo> findByName(String name) {
		List<DvdInfo> list = new ArrayList<DvdInfo>();
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="Select *  from dvdInfo where name like ? ";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			psm.setString(1, "%"+name+"%");
			//执行
			rs = psm.executeQuery();//查询==》返回的是，虚拟表==》sql的运行结果
			//处理返回结果
			while (rs.next()){
				DvdInfo info = new DvdInfo(
						rs.getInt("id"),
						rs.getString("state"),
						rs.getString("name"),
						rs.getString("borrowDate"),
						rs.getInt("borrowCount")
				);
				list.add(info);//添加到集合
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return list;
	}

	@Override
	public int add(DvdInfo info) {
		int ret = 0;
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="insert into dvdInfo(state,  name,  borrowDate,  borrowCount) values(?,?,?,?)";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			psm.setString(1, info.getState());
			psm.setString(2, info.getName());
			psm.setString(3, info.getBorrowDate());
			psm.setInt(4, info.getBorrowCount());
			//执行
			ret = psm.executeUpdate();//增、删、改
			//处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return ret;
	}

	@Override
	public int delById(Integer id) {
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="delete from dvdInfo where id=?";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			psm.setInt(1, id);
			//执行
			int ret = psm.executeUpdate();//增、删、改
			//处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return 0;
	}

	@Override
	public int update(DvdInfo info) {
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="update dvdInfo set  state=?,  name=?,  borrowDate=?,  borrowCount=? " +
				"where id=?";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			psm.setString(1, info.getState());
			psm.setString(2, info.getName());
			psm.setString(3, info.getBorrowDate());
			psm.setInt(4, info.getBorrowCount());
			psm.setInt(5, info.getId());
			//执行
			int ret = psm.executeUpdate();//增、删、改
			//处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return 0;
	}

	@Override
	public int modify(int id, String time) {
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="update dvdInfo set  state='已借出',  borrowDate=?,  borrowCount=borrowCount+1 " +
				"where id=?";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			psm.setString(1, time);
			psm.setInt(2, id);
			//执行
			int ret = psm.executeUpdate();//增、删、改
			//处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(rs, psm, conn);
		}
		return 0;
	}
}
